<?php
/**
 * Created by PhpStorm.
 * User: denh
 * Date: 16/9/6
 * Time: 上午11:27
 */
require_once _LHDir . 'bs/BsUserManage.php';

class BsUserCenter extends Base {

    public static $hproseHttpClient;

    function __construct() {
        parent::__construct();
        $this->load_hprose();
    }

    function load_hprose($base_url = USER_CENTER_BASE_URL) {
        require_once _LHDir . 'sdk/hprose-php-master/src/Hprose.php';
        self::$hproseHttpClient = new \HproseHttpClient($base_url);
    }

    function getInfoFromUserCenter($openId) {
        if (!$openId) {
            logger()->logInfo('===openId is null===');###
            return;
        }
        $doUser = new Do用户();
        $doUser->set_openID($openId);
        if (!$doUser->where($doUser->data)->row()) {
            $re = self::$hproseHttpClient->UserCenterController_tourist($openId, '', 'weixin', '', $openId); //创建游客
        } else {
            $phone = $doUser->get_telephone();
            if (!$phone) $phone = '';

            $re = self::$hproseHttpClient->UserCenterController_wxGetUserInfo($openId, $phone);
            logger()->logInfo('UserCenterController_wxGetUserInfo'.var_export($re, true));###
        }


        $this->writeSession($re, $doUser);

        #如果用户在用户中心已经通过认证，则同步到公众号数据库
        $infos = $re->result;
        if ($infos->author->status == '1') {
            $openId = $infos->account->openId;
            $data = array(
                'name' => $infos->author->name,
                'avatar' => $infos->author->icon,
                'status' => $infos->author->status,
                'keshi' => $infos->author->sector,
                'zhicheng' => $infos->author->title,
                'hospital' => $infos->author->hospital
            );
            BsUserManage::modifyUserInfo($openId, 'openid', $data);
        }

        logger()->logInfo('UserCenterController_wxGetUserInfo--session'.var_export($_SESSION, true));###
        #设置用户中心头像性别
        if (!$_SESSION['userCenter']['icon']) {
            $doUser = new Do用户();
            $doUser->set_openID($openId);
            $note = $doUser->fields('note')->where($doUser->data)->getOneField();
            $note = json_decode($note, true);
            if ($note) {
                if ($note['sex'] == 1) {
                    $sex = 0;
                } elseif ($note['sex'] == 2) {
                    $sex = 1;
                } else {
                    $sex = -1;
                }
                $age = $_SESSION['userCenter']['age'] ? $_SESSION['userCenter']['age'] : 0;

                #设置头像
                $re_1 = self::$hproseHttpClient->UserCenterController_setBaseInfo($_SESSION['userCenter']['token'],
                    $note['headimgurl'], $sex, $age);
                logger()->logInfo('UserCenterController_setBaseInfo'.var_export($re_1, true));###
            }
        }
    }

    function writeSession($re, Do用户 $doUser = null) {
        if ($doUser && !$doUser->get_userCenterID() && $doUser->get_id()) {
            if (isset($re->result->account->id)) {
                $doUser->set_userCenterID($re->result->account->id);
                $doUser->update();
            }
        }
        $_SESSION['userCenter']['id'] = $re->result->account->id;
        $_SESSION['userCenter']['token'] = $re->result->account->token;
        $_SESSION['userCenter']['icon'] = $re->result->author->icon;
        $_SESSION['userCenter']['sex'] = $re->result->author->sex;
        $_SESSION['userCenter']['age'] = $re->result->author->age;
    }
}